package com.yx.fx.webApi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yx.fx.sys.db.entity.SysDepartment;
import com.yx.fx.sys.db.mapper.SysDepartmentMapper;
import com.yx.fx.webApi.entity.BankMangerEnterprise;
import com.yx.fx.webApi.mapper.BankManagerEnterpriseMapper;
import com.yx.fx.webApi.param.EnterpriseAndDepartmentParam;
import com.yx.fx.webApi.param.TEnterpriseNameParam;
import com.yx.fx.webApi.serviceImpl.TEnterpriseServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pxb
 * @since 2021-09-17
 */
@RestController
@RequestMapping("sys/tEnterprise")
public class TEnterpriseController {

    @Resource
    private TEnterpriseServiceImpl tEnterpriseServiceImpl;

    @Resource
    private BankManagerEnterpriseMapper bankManagerEnterpriseMapper;

    @ApiOperation("分页")
    @PostMapping("/getList")
    public List<BankMangerEnterprise> getList(@RequestBody TEnterpriseNameParam param) {
        QueryWrapper<BankMangerEnterprise> wrapper = new QueryWrapper<>();
        wrapper.like("enterprise_name", param.getName());
        List<BankMangerEnterprise> enterprises = bankManagerEnterpriseMapper.selectList(wrapper);
        System.out.println(enterprises);
        return enterprises;
    }


    @Resource
    private SysDepartmentMapper sysDepartmentMapper;

    @RequestMapping(value = "/allocateAndSave", method = RequestMethod.POST)
    public void allocateAndSave(@RequestBody EnterpriseAndDepartmentParam params) {
        System.out.println(params.getEnterpriseId());
        System.out.println(params.getDepartmentId());
        System.out.println(params.getSysUserName());
        BankMangerEnterprise bankMangerEnterprise = bankManagerEnterpriseMapper.selectOne(new QueryWrapper<BankMangerEnterprise>().eq("enterprise_id", params.getEnterpriseId()));
        if (bankMangerEnterprise != null) {
            UpdateWrapper<BankMangerEnterprise> wrapper = new UpdateWrapper<>();
            wrapper.eq("enterprise_id", params.getEnterpriseId());
            wrapper.set("department_id", params.getDepartmentId());
            SysDepartment sysDepartment = sysDepartmentMapper.selectOne(new QueryWrapper<SysDepartment>().eq("id", params.getDepartmentId()));
            wrapper.set("department_name",sysDepartment.getName() );
            wrapper.set("department_address",sysDepartment.getAddress());
            wrapper.set("sys_user_name", params.getSysUserName());
            bankManagerEnterpriseMapper.update(null, wrapper);
        }
    }


}

